class LastJiFenSingleAwardView extends egret.DisplayObjectContainer{

    public static pool:LastJiFenSingleAwardView[] = [];

    private _iconImg:eui.Image; // 图标
    private _nameLabel:BaseTextField;// 名称
    private _descLabel:BaseTextField; // 描述
    private _numLabel:BaseTextField; // 个数

    constructor(){
        super();
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.viewDidLoad, this);
    }

    private viewDidLoad(){
        this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.viewDidDisappear, this);
        // 1.图片
        if(!this.contains(this.iconImg)) this.addChild(this.iconImg);
        // 2.名称
        if(!this.contains(this.nameLabel)) this.addChild(this.nameLabel);
        // 3. 描述
        if(!this.contains(this.descLabel)) this.addChild(this.descLabel);
        // 4. 数量
        if(!this.contains(this.numLabel)) this.addChild(this.numLabel);

        // 刷新自身的高度
        this.height = utils.getMaxY(this.descLabel) + this.nameLabel.y;
        this.iconImg.y = this.height / 2.0;
    }

    private viewDidDisappear(){
        this.removeEventListener(egret.Event.REMOVED_FROM_STAGE, this.viewDidDisappear, this);
        LastJiFenSingleAwardView.pool.push(this);
    }

    /*********************************************** public **********************************************************/
    public static getObj():LastJiFenSingleAwardView{
        var obj = this.pool.shift();
        if(!obj){
            obj = new LastJiFenSingleAwardView();
        }
        return obj;
    }

    /*********************************************** private **********************************************************/
    private receiveImgAction(){
        if(!this.iconImg.texture) return;
        this.iconImg.height =  this.iconImg.width * this.iconImg.texture.textureHeight / this.iconImg.texture.textureWidth;
        this.iconImg.anchorOffsetY = this.iconImg.height / 2.0;
        this.numLabel.y = utils.getMaxY(this.iconImg) - this.numLabel.height - 5;

    }

    /*********************************************** setter **********************************************************/
    public set iconUrl(value: string) {
        this.iconImg.source = value;
    }

    public set name(value:string){
        this.nameLabel.text = value;
    }

    public set describtion(value:string){
        this.descLabel.text = value;
    }

    public set num(value:number){
        this.numLabel.text = "X" + value;
        this.numLabel.x = utils.getMaxX(this.iconImg) - this.numLabel.width + 10;
    }

    /*********************************************** getter **********************************************************/
    
    private get numLabel():BaseTextField{
        if(!this._numLabel){
            var numLabel = new BaseTextField();
            numLabel.textColor = 0xFED721;
            numLabel.strokeColor = 0x885615;
            numLabel.stroke = 2;
            numLabel.italic = true;
            numLabel.text = "X";
            numLabel.bold = true;
            numLabel.x = utils.getMaxX(this.iconImg) - numLabel.width;
            numLabel.y = utils.getMaxY(this.iconImg) - numLabel.height - 5;
            this._numLabel = numLabel;
        }
        return this._numLabel;
    }

    private get descLabel():BaseTextField{
        if(!this._descLabel){
            var descLabel = new BaseTextField();
            descLabel.textColor = 0x633E0C;
            descLabel.size = 24;
            descLabel.x = this.nameLabel.x;
            descLabel.width = this.width - descLabel.x;
            descLabel.verticalAlign = egret.VerticalAlign.MIDDLE;
            descLabel.y = utils.getMaxY(this.nameLabel) + 5;
            this._descLabel = descLabel;
        }
        return this._descLabel;
    }

    private get nameLabel():BaseTextField{ 
        if(!this._nameLabel){
            var nameLabel = new BaseTextField();
            nameLabel.textColor = 0x633E0C;
            nameLabel.size = 24;
            nameLabel.x = utils.getMaxX(this.iconImg) + 20;
            nameLabel.width = this.width - nameLabel.x;
            nameLabel.height = 30;
            nameLabel.y = 20;
            nameLabel.verticalAlign = egret.VerticalAlign.MIDDLE;
            nameLabel.bold = true;
            this._nameLabel = nameLabel;
        }
        return this._nameLabel;
    }

    private get iconImg():eui.Image{
        if(!this._iconImg){
            var iconImg = new eui.Image();
            iconImg.width = 80;
            iconImg.addEventListener(egret.Event.COMPLETE, this.receiveImgAction, this);
            iconImg.y = this.height  /2.0;
            this._iconImg = iconImg;
        }
        return this._iconImg;
    }
}